package com.ruihu.admin.module.business.electric.config.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruihu.admin.common.constant.ResponseCodeConst;
import com.ruihu.admin.common.domain.PageResultDTO;
import com.ruihu.admin.common.domain.ResponseDTO;
import com.ruihu.admin.constant.ElectricConfigTypeEnum;
import com.ruihu.admin.module.business.electric.config.constant.ElectricConfigResponseCodeConst;
import com.ruihu.admin.module.business.electric.config.dao.ElectricConfigDao;
import com.ruihu.admin.module.business.electric.config.domain.dto.ElectricConfigAddDTO;
import com.ruihu.admin.module.business.electric.config.domain.dto.ElectricConfigDTO;
import com.ruihu.admin.module.business.electric.config.domain.dto.ElectricConfigUpdateDTO;
import com.ruihu.admin.module.business.electric.config.domain.dto.ElectricConfigQueryDTO;
import com.ruihu.admin.module.business.electric.config.domain.entity.ElectricConfigEntity;
import com.ruihu.admin.module.business.electric.config.domain.vo.ElectricConfigVO;
import com.ruihu.admin.module.business.electric.config.domain.vo.ElectricConfigExcelVO;
import com.ruihu.admin.module.business.electric.config.domain.vo.ElectricTypeVO;
import com.ruihu.admin.module.business.electric.config.manager.ElectricConfigManager;
import com.ruihu.admin.module.system.user.UserService;
import com.ruihu.admin.module.system.user.domain.bo.UserBO;
import com.ruihu.admin.util.BeanCopierUtil;
import com.ruihu.admin.util.PageUtil;
import com.ruihu.admin.util.BeanUtil;
import com.ruihu.admin.util.RequestTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * [ 电芯型号维护表 ]
 *
 * @author Tony
 * @version 1.0
 * @company 瑞虎（南京）信息技术有限公司
 * @copyright (c)  瑞虎（南京）信息技术有限公司Inc. All rights reserved.
 * @date 2022-12-28 12:14:15
 * @since JDK1.8
 */
@Service
public class ElectricConfigService {

    @Autowired
    private ElectricConfigDao electricConfigDao;

    @Autowired
    private UserService userService;

    @Autowired
    private ElectricConfigManager electricConfigManager;

    /**
     * 根据id查询
     */
    public ElectricConfigEntity getById(Long id){
        return  electricConfigDao.selectById(id);
    }

    /**
     * 分页查询
     * @author Tony
     * @date 2022-12-28 12:14:15
     */
    public ResponseDTO<PageResultDTO<ElectricConfigVO>> queryByPage(ElectricConfigQueryDTO queryDTO) {
        Page page = PageUtil.convert2QueryPage(queryDTO);
        List<ElectricConfigVO> voList = electricConfigDao.queryByPage(page, queryDTO).stream().map(this::buildElectricConfigVO)
                .collect(Collectors.toList());;
        page.setRecords(voList);
        PageResultDTO<ElectricConfigVO> pageResultDTO = PageUtil.convert2PageResult(page);
        return ResponseDTO.succData(pageResultDTO);
    }

    private ElectricConfigVO buildElectricConfigVO(ElectricConfigVO electricConfigVO) {
        if(electricConfigVO == null){
            return null;
        }
        if (Objects.nonNull(electricConfigVO.getCreator())) {
            UserBO createUser = this.userService.getById(electricConfigVO.getCreator());
            if (Objects.nonNull(createUser)) {
                electricConfigVO.setCreateByName(createUser.getActualName());
            }
        }
        if (Objects.nonNull(electricConfigVO.getUpdater())) {
            UserBO updateUser = this.userService.getById(electricConfigVO.getUpdater());
            if (Objects.nonNull(updateUser)) {
                electricConfigVO.setUpdateByName(updateUser.getActualName());
            }
        }
        return electricConfigVO;
    }

    /**
     * 添加
     * @author Tony
     * @date 2022-12-28 12:14:15
     */
    public ResponseDTO<String> add(ElectricConfigAddDTO addDTO) {
        ElectricConfigDTO electricConfigDTO = electricConfigDao.getElectricConfigByCodeAndType(addDTO.getCode(),
                addDTO.getConfigType());
        if (Objects.nonNull(electricConfigDTO)) {
            return ResponseDTO.wrap(ElectricConfigResponseCodeConst.TYPE_HAS_EXISTS);
        }
        ElectricConfigEntity entity = BeanUtil.copy(addDTO, ElectricConfigEntity.class);
        //entity.setConfigType(ElectricConfigTypeEnum.ELECTRIC_CORE_TYPE_CONFIG.getCode());
        entity.setCreator(RequestTokenUtil.getRequestUser().getRequestUserId());
        entity.setCreateTime(new Date());
        electricConfigDao.insert(entity);
        return ResponseDTO.succ();
    }

    /**
     * 编辑
     * @author Tony
     * @date 2022-12-28 12:14:15
     */
    public ResponseDTO<String> update(ElectricConfigUpdateDTO updateDTO) {
        ElectricConfigDTO electricConfigDTO = electricConfigDao.getElectricConfigByCodeAndType(updateDTO.getCode(),
                updateDTO.getConfigType());
        if (Objects.nonNull(electricConfigDTO)&&!Objects.equals(updateDTO.getId(),electricConfigDTO.getId())) {
            return ResponseDTO.wrap(ElectricConfigResponseCodeConst.TYPE_HAS_EXISTS);
        }
        electricConfigDTO = new ElectricConfigDTO();
        BeanCopierUtil.copy(updateDTO, electricConfigDTO);
        this.electricConfigManager.updateAndCacheIt(electricConfigDTO);
        return ResponseDTO.succ();
    }


    /**
     * 删除
     * @author Tony
     * @date 2022-12-28 12:14:15
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> deleteByIds(List<Long> idList) {
        electricConfigDao.deleteByIdList(idList);
        return ResponseDTO.succ();
    }

    /**
     * 删除
     * @author Tony
     * @date 2022-12-28 12:14:15
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> deleteById(Long id) {
        electricConfigDao.deleteById(id);
        return ResponseDTO.succ();
    }

    /**
     * 查询全部导出对象
     * @author Tony
     * @date 2022-12-28 12:14:15
     */
    public List<ElectricConfigExcelVO> queryAllExportData(ElectricConfigQueryDTO queryDTO) {
        return electricConfigDao.queryAllExportData( queryDTO);
    }

    /**
     * 批量查询导出对象
     * @author Tony
     * @date 2022-12-28 12:14:15
     */
    public List<ElectricConfigExcelVO> queryBatchExportData(List<Long> idList) {
        return electricConfigDao.queryBatchExportData(idList);
    }

    /**
     * 查询电芯信息
     * @author Tony
     * @date 2022-12-28 12:14:15
     */
    public ResponseDTO<List<ElectricTypeVO>> queryElectricConfig(ElectricConfigTypeEnum electricConfigTypeEnum) {
        return ResponseDTO.succData(electricConfigDao.queryElectricType(electricConfigTypeEnum.getCode()));
    }

    /**
     * 根据Code查询电芯型号
     * @param code
     * @return
     */
    @Cacheable(value = "getElectricConfigByCode", key = "T(com.ruihu.admin.constant.RedisConstants).queryElectricConfigByCodeKey(#code)", condition = "#code != null", unless = "#result.getData() == null")
    public ResponseDTO<ElectricConfigDTO> getElectricConfigByCode(String code) {
        ElectricConfigDTO electricConfigDTO = this.electricConfigDao.getElectricConfigByCodeAndConfigType(code, ElectricConfigTypeEnum.ELECTRIC_CORE_TYPE_CONFIG.getCode());
        if (Objects.nonNull(electricConfigDTO))
            return ResponseDTO.succData(electricConfigDTO);
        return ResponseDTO.wrap(ResponseCodeConst.NOT_EXISTS);
    }
}
